<?php
  header("content-type:text/html;charset=utf8"); 
  //用户在微信绑定设备 进行账户注册并与微信进行绑定
  $tel = $_POST['tel'];
  $password = md5($_POST['password']);
  $openid = $_POST['openid'];
  if (!$openid) {
   echo "<script>alert('找回账户密码失败，可能因为网络原因导致，请关闭网页进行扫码重试');location.href='https://pay.hispot.cn/gitscan/h5/login.php'</script>";
   exit;
  }

  //查询新数据库判断数据是否存在
  $pdo = pdo_connect_new_database();
  $select_new_sql = "SELECT * FROM user WHERE tel = '$tel'";
  $login_data = $pdo->query($select_new_sql)->fetch(PDO::FETCH_ASSOC);
  if ($login_data) {
    new_data_existence($tel,$password,$openid,$pdo);
  }else{
    new_data_no_existence($tel,$password,$openid,$pdo);
  }

  //新数据库不存在此条数据，查询旧数据库，数据存在修改密码，并且数据同步，不存在告知用户此号码不存在
  function new_data_no_existence($tel,$password,$openid,$pdo){
    //查询旧数据库
    $pdo_user_old = pdo_connect_old_user_database();
    $select_old_user_sql = "SELECT * FROM uc_members WHERE username = '$tel'";
    $old_user_data = $pdo_user_old->query($select_old_user_sql)->fetch(PDO::FETCH_ASSOC);
   
    //判断数据是否存在
    if ($old_user_data) {
      //数据存在
      $old_password = md5($password.$old_user_data['salt']);
      //判断新旧密码是否一致
      if ($old_password == $old_user_data['password']) {
        echo "<script>alert('新密码与旧密码不能一致');location.href='https://pay.hispot.cn/gitscan/h5/back.php?openid=".$openid."'</script>";
        exit;
      }
      //修改旧数据库中的密码
      $update_old_user_sql = "UPDATE uc_members SET password = '$old_password' WHERE username = '$tel'";
      $res = $pdo_user_old->exec($update_old_user_sql);
      if ($res) {        
        //获取新数据库中的绑定信息
        $select_binding_sql = "SELECT * FROM binding WHERE openid = '$openid' ORDER BY b_id DESC";
        $new_binding_data = $pdo->query($select_binding_sql)->fetch(PDO::FETCH_ASSOC);

        //将新数据库的绑定数据同步到旧绑定数据库中
        $pdo_binding_old = pdo_connect_old_binidng_database();
        $uid = $old_user_data['uid'];
        $time = date("Y-m-d H:i:s");
        $sn = $new_binding_data['device_sn'];
        $insert_old_binidng_sql = "INSERT INTO weixin_binging(Uid,OpinID,CreateTime,DeviceSelf) VALUES('$uid','$openid','$time','$sn')";
        $pdo_binding_old->exec($insert_old_binidng_sql);

        //然后通过uid以及openid来查询关联数据是否存在
        $select_relation_data_sql = "SELECT * FROM weixin_user WHERE openId = '$openid' AND user = '$uid'";
        $relation_data = $pdo_binding_old->query($select_relation_data_sql)->fetch(PDO::FETCH_ASSOC);
        //如果数据不存在
        if (!$relation_data) {
          //将uid和openid关联存在weixin_user表中
          $insert_old_relation_sql = "INSERT INTO weixin_user(openId,user) VALUES('$openid','$uid')";
          $pdo_binding_old->exec($insert_old_relation_sql); 
        }

        //最后将旧数据库的登录信息同步到新数据库中
        $insert_new_user_sql = "INSERT INTO user(tel,password,openid,`time`) VALUES('$tel','$password','$openid','$time')";
        $pdo->exec($insert_new_user_sql);  

        echo "<script>alert('恭喜您已经成功找回密码并且完成绑定操作，现在去设备端进行登录吧');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
        exit;
      }else{
        //数据修改失败
        echo "<script>alert('找回账户密码失败，可能因为网络原因导致，请进行重试');location.href='https://pay.hispot.cn/gitscan/h5/back.php?openid=".$openid."'</script>";
        exit;
      }
    }else{
      //数据不存在
      echo "<script>alert('找回账户密码失败，可能因为网络原因导致，请进行重试');location.href='https://pay.hispot.cn/gitscan/h5/back.php?openid=".$openid."'</script>";
      exit;
    }
  }


  //新数据库存在此条数据，进行修改密码，同步所有数据到旧数据库
  function new_data_existence($tel,$password,$openid,$pdo){
    //修改新数据库的数据
    $update_new_sql = "UPDATE user SET password = '$password',openid = '$openid' WHERE tel = '$tel'";
    $res = $pdo->exec($update_new_sql);
    if (!$res) {
      echo "<script>alert('找回账户密码失败，可能因为网络原因导致，请进行重试');location.href='https://pay.hispot.cn/gitscan/h5/back.php?openid=".$openid."'</script>";
      exit;
    }

    //将登陆信息同步到旧数据库
    $pdo_user_old = pdo_connect_old_user_database();
    $sql = "SELECT * FROM uc_members WHERE username = '$tel'";
    $data = $pdo_user_old->query($sql)->fetch(PDO::FETCH_ASSOC);
    if ($data) {
          // $delete_sql = "DELETE FROM uc_members WHERE username = '$tel'";
          // $pdo_user_old->exec($delete_sql);
          $uid = $data['uid'];
    }else{
          $salt = substr(md5(rand('1','1000')),0,6);
          $old_password = md5($password.$salt);
          $insert_old_user_sql = "INSERT INTO uc_members(username,password,salt,phone) VALUES('$tel','$old_password','$salt','$tel')";
          $pdo_user_old->exec($insert_old_user_sql);
          $uid = $pdo_user_old->lastInsertId();
    }

    //获取新数据库里面的绑定信息
    $select_binding_sql = "SELECT * FROM binding WHERE openid = '$openid'";
    $new_binding_data = $pdo->query($select_binding_sql)->fetch(PDO::FETCH_ASSOC);

    //将新数据的绑定信息同步旧数据库的绑定信息表中
    $pdo_binding_old = pdo_connect_old_binidng_database();
    $sn = $new_binding_data['device_sn'];
    $time = date("Y-m-d H:i:s");
    $select_old_binding_sql = "SELECT * FROM weixin_binging WHERE Uid = '$uid' AND OpinID = '$openid' AND DeviceSelf = '$sn'";
    $old_binding_data = $pdo_binding_old->query($select_old_binding_sql)->fetch(PDO::FETCH_ASSOC);
    if (!$old_binding_data) {
        $insert_old_binidng_sql = "INSERT INTO weixin_binging(Uid,OpinID,CreateTime,DeviceSelf) VALUES('$uid','$openid','$time','$sn')";
        $pdo_binding_old->exec($insert_old_binidng_sql);   
    }

    //最后将uid和openid关联添加weixin_user表中 添加之前先通过uid以及openid来查询关联数据是否已经存在
      $select_relation_data_sql = "SELECT * FROM weixin_user WHERE openId = '$openid' AND user = '$uid'";
      $relation_data = $pdo_binding_old->query($select_relation_data_sql)->fetch(PDO::FETCH_ASSOC);
      //如果数据不存在
      if (!$relation_data) {
        //将uid和openid关联存在weixin_user表中
        $insert_old_relation_sql = "INSERT INTO weixin_user(openId,user) VALUES('$openid','$uid')";
        $pdo_binding_old->exec($insert_old_relation_sql); 
      }
    file_get_contents("https://pay.hispot.cn/gitscan/pushLoginInfo.php?openid=".$openid);
    echo "<script>alert('恭喜您已经成功找回密码并且完成绑定操作，现在去设备端进行登录吧');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
    exit;
  }


  //pdo连接新数据库
  function pdo_connect_new_database(){
    $pdo_new = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
    $pdo_new->exec('set names utf8'); 
    return $pdo_new;
  }


  //pdo连接旧用户登录信息库
  function pdo_connect_old_user_database(){
    $pdo_user_old = new PDO('mysql:host=123.57.16.97;dbname=ucenter','root','zqVnUy6cxsQXbpEZ');
    $pdo_user_old->exec('set names utf8'); 
    return $pdo_user_old;
  }


  //pdo连接旧用户绑定表
  function pdo_connect_old_binidng_database(){
    $pdo_binding_old = new PDO('mysql:host=123.57.16.97;dbname=wx_online_movie','root','zqVnUy6cxsQXbpEZ');
    $pdo_binding_old->exec('set names utf8');
    return $pdo_binding_old;
  }
   // $pdo = new PDO('mysql:host=123.57.16.97;dbname=device_binding','root','zqVnUy6cxsQXbpEZ');
   // $pdo->exec('set names utf8');
   // $uid = old_user_insert($tel,$password);
   // old_binding_insert($openid,$uid,$pdo);
   // $time = date('Y-m-d H:i:s');
   // $sql = "UPDATE user SET password = '$password',openid = '$openid' WHERE tel = '$tel'";
   // $res = $pdo->exec($sql);
   // if ($res) {
   //      echo "<script>alert('恭喜您已经成功找回密码并且完成绑定操作，现在去设备端进行登录吧');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
   // }else{
   //      $time = date("Y-m-d H:i:s");
   //      $insert_sql = "INSERT INTO user(tel,password,openid,`time`) VALUES('$tel','$password','$openid','$time')";
	  //     $insert_res = $pdo->exec($insert_sql);
   //      if ($insert_res) {
   //          echo "<script>alert('恭喜您已经成功找回密码并且完成绑定操作，现在去设备端进行登录吧');location.href='https://pay.hispot.cn/gitscan/h5/success.php'</script>";
   //      }else{
   //          echo "<script>alert('找回账户密码失败，可能因为网络原因导致，请关闭网页进行扫码重试');location.href='https://pay.hispot.cn/gitscan/h5/back.php?openid=".$openid."'</script>";
   //      }
   // }

   // //将新注册的数据存放原数据库中
   // function old_user_insert($tel,$password){
   //    $old_pdo_user = new PDO('mysql:host=123.57.16.97;dbname=ucenter','root','zqVnUy6cxsQXbpEZ');
   //    $old_pdo_user->exec('set names utf8');
   //    $sql = "SELECT * FROM uc_members WHERE username = ".$tel;
   //    $data = $old_pdo_user->query($sql)->fetch(PDO::FETCH_ASSOC);
   //    if ($data) {
   //      $old_password = md5($password.$data['salt']);
   //      $update_sql = "UPDATE uc_members SET password = '$old_password' WHERE username = '$tel'";
   //      $res = $old_pdo_user->exec($update_sql);
   //        return $data['uid'];
   //    }else{
   //      $salt = substr(md5(rand('1','1000')),0,6);
   //      $old_password = md5($password.$salt);
   //      $insert_user_sql = "INSERT INTO uc_members(username,password,salt,phone) VALUES('$tel','$old_password','$salt','$tel')";
   //      $res = $old_pdo_user->exec($insert_user_sql);
   //      if ($res) {
   //        return $old_pdo_user->lastInsertId();
   //      }else{
   //        return 'NO';
   //      }
   //    }
   // }

   // //将微信绑定设备号的数据放到元数据库中
   // function old_binding_insert($openid,$uid,$pdo){
   //  if ($uid == 'NO') {
   //     echo "<script>alert('注册账户失败，可能因为网络原因导致，请关闭网页进行扫码重试');location.href='https://pay.hispot.cn/gitscan/h5/register.php?openid=".$openid."'</script>";
   //     exit;
   //  }
   //    $old_pdo_binding = new PDO('mysql:host=123.57.16.97;dbname=wx_online_movie','root','zqVnUy6cxsQXbpEZ');
   //    $old_pdo_binding->exec('set names utf8');
   //    $select_sn_sql = "SELECT * FROM binding WHERE openid = '$openid'";
   //    $sn_data = $pdo->query($select_sn_sql)->fetch(PDO::FETCH_ASSOC);
   //    $time = date("Y-m-d H:i:s");
   //    $insert_binding_sql = "INSERT INTO weixin_binging(Uid,OpinID,CreateTime,DeviceSelf) VALUES('$uid','$openid','$time','".$sn_data['device_sn']."')";
   //    $res = $old_pdo_binding->exec($insert_binding_sql);
   //    if (!$res) {
   //      $old_pdo_user = new PDO('mysql:host=123.57.16.97;dbname=ucenter','root','zqVnUy6cxsQXbpEZ');
   //      $old_pdo_user->exec('set names utf8');
   //      $delete_user_sql = "DELETE FROM uc_members WHERE uid = '$uid'";
   //      $old_pdo_user->exec($delete_user_sql);
   //      echo "<script>alert('存微信和注册账户失败，可能因为网络原因导致，请关闭网页进行扫码重试设备号关联的时候错了');location.href='https://pay.hispot.cn/gitscan/h5/register.php?openid=".$openid."'</script>";
   //     exit;
   //    }
   // }
 ?>
